package com.xw.Mail.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import com.xw.Beans.MailBox;
import com.xw.Beans.SendingMail;
import com.xw.Mail.Sender.CommonMailSender;
import com.xw.Mail.listener.SendMailTransportListener;

/**
 * 描述:封装发送邮件的工具类
 * @author 杨建全
 * @date 2017年6月7日 下午3:46:52
 */
@Component
public class MailSendUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(MailSendUtil.class);
	@Autowired
	private SendMailTransportListener listener;
	/**
	 * 描述：异步发送邮件
	 * @author 杨建全
	 * @date 2017年6月7日
	 * @param mail
	 * @param mailBox
	 */
	@Async
	public void toSendMailAsync(SendingMail mail, MailBox mailBox){
		try {
			// 获取配置的信息
			CommonMailSender sender = new CommonMailSender(mailBox,listener);
			mail.setFromUser(mailBox.getMailBoxName());
			sender.sendMailAsync(mail);
		} catch (Exception e) {
			logger.error("发送邮件【" + mail.getSubject() + "】发送异常！", e);
		}
	}
}
